<?php
class TeamController extends AppController {

	public $name = 'team';
	function beforeFilter() {
	    parent::beforeFilter(); 
		$this->C_auth->autoRedirect = false; //For override login
	    $this->C_auth->allowedActions = array('department','deldep','staff','delstaff','remuneration','delrem','login','logout','teampwd','teampwdedit','teampwdlogin');
	}
	
	public function deldep() {	
        $address_id=intval(Request::get_args('address_id'));
		$query = array(
            'conditions' => array('department.shop_id' => $this->my->id,'department.id' => $address_id),
			'fields' => array('department.*'), 
		);
		$address_rs = POP::exe('[@@:UM department][@@:QU find:first]',$query);
		if(empty($address_rs)) {
            $this->flash("该项不存在!", Router::url('team/department/'));		
		}else {
		    $this->department->delete($address_id);
            $this->flash("执行成功！", Router::url('team/department/'));		   
		}        		
	}

	public function department() {
        $this->_teamRights();	
        $address_id=intval(Request::get_args('address_id'));	
        if(!empty($this->data)) {
		        if(!isset($_POST['rights']))$_POST['rights']=array();
                $rights = $_POST['rights'];
				$rightsstring = '';
                foreach($rights as $v) {
                    $rightsstring .=$v.',';
                }				
                if($this->data['department']['id']) {		
					$address_id = $this->data['department']['id'];
			    }

				$query = array(
		            'conditions' => array('department.name' => $this->data['department']['name'],'department.shop_id' => $this->my->id),
					'fields' => array('department.*'), 
				);
		        $isarray = POP::exe('[@@:UM department][@@:QU find:first]',$query);
		        
			    if(empty($isarray) || $address_id) {	
			        $this->data['department']['shop_id'] = $this->my->id;	
			        $this->data['department']['rights'] = $rightsstring;
			        unset($this->data['department']['id']);					
			        $safeData = $this->data['department'];
			        $query = array();			        			    
	                if($address_id) {		
						$query['keyid'] = $address_id;
						$query['set'] = $safeData;
				    }else {
                        $query = $safeData;
					}				    			    				
		            POP::exe('[@@:UM department][@@:QU save]',$query);
                    $this->flash("执行成功！", Router::url('team/department/'));						
                }else {
                     $this->flash("该部门名称已经存在！", Router::url('team/department/'));
                }					
        }else {	

		$query = array(
            'conditions' => array('department.shop_id' => $this->my->id),
			'fields' => array('department.*'), 
		);
		$address_rs = POP::exe('[@@:UM department][@@:QU find:all]',$query);
	
        $user_info=array(
	        'name'=>'',
	        'telphone'=>'',
	        'address'=>'',
	        'responsible'=>'',
	        'supervisor'=>'',
	        'assistant'=>'',
	        'superior_leadership'=>'',
	        'charge_leadership'=>'',
	        'ddepartment'=>'',			
        );		
        if($address_id && $address_rs){
	        foreach($address_rs as $value) {
		        if($address_id == $value['department']['id']) {
			        $user_info = $value['department'];
		        }
	        }
        }	
        $this->set('user_info',$user_info);		
        $this->set('address_rs',$address_rs);	
        $this->set('address_id',$address_id);
		}
	}	

	
	public function delstaff() {	
        $this->_teamRights();	
        $address_id=intval(Request::get_args('address_id'));

		$query = array(
            'conditions' => array('staff.shop_id' => $this->my->id,'staff.id' => $address_id),
			'fields' => array('staff.*'), 
		);
		$address_rs = POP::exe('[@@:UM staff][@@:QU find:first]',$query);

		if(empty($address_rs)) {
            $this->flash("该项不存在!", Router::url('team/staff/'));		
		}else {
		    $this->staff->delete($address_id);
            $this->flash("执行成功！", Router::url('team/staff/'));		   
		}        		
	}		
	
	public function staff() {	
	    $this->_teamRights();
        $address_id=intval(Request::get_args('address_id'));

		$query = array(
            'conditions' => array('department.shop_id' => $this->my->id),
			'fields' => array('department.*'), 
		);
		$cat_rs = POP::exe('[@@:UM department][@@:QU find:all]',$query);

		$departmentcat = array();
	    foreach($cat_rs as $value) {
			$departmentcat[$value['department']['id']] = $value['department']['name'];
	    } 		
        if(!empty($this->data)) {	

			$query = array(
	            'conditions' => array('staff.staff_num' => $this->data['staff']['staff_num'],'staff.shop_id' => $this->my->id),
				'fields' => array('staff.*'), 
			);
		    $isarray = POP::exe('[@@:UM staff][@@:QU find:first]',$query);

			if(empty($isarray) || $address_id) {
			    if(!empty($this->data['staff']['department_id'])&&!empty($this->data['staff']['staff_pwd'])) {			
                Loader::import('lib.cupload');		
	            $up = new upload('jpg|gif|png|jpge',1024, 'attach');
	            $up->set_dir(APP_ROOT . Param::read('upfiles.path') . '/teamstaff/','{y}/{m}/{d}');
	            $file = $up->execute();	
				$rootpath = trim(str_replace(Param::read('wwwrootPatch'),"",Param::read('htdocPatch')),'/') ;
                $rootpath = !empty($rootpath)?	('/'.$rootpath) : '';				
                if($file) {
                    $realtxt=$file[0];
		            $this->data['staff']['logo'] =  $rootpath. str_replace(APP_ROOT,"",$realtxt['dir']).$realtxt['name'];				
                }
	            $up = new upload('jpg|gif|png|jpge',1024, 'attach1');
	            $up->set_dir(APP_ROOT . Param::read('upfiles.path') . '/teamstaff/','{y}/{m}/{d}');
	            $file = $up->execute();	
                if($file) {
                    $realtxt=$file[0];
		            $this->data['staff']['jobstr'] =  $rootpath. str_replace(APP_ROOT,"",$realtxt['dir']).$realtxt['name'];				
                }
                
				$query = array(
		            'conditions' => array('shopinfo.user_id' => $this->my->id),
					'fields' => array('shopinfo.*'), 
				);
                $shop_info = POP::exe('[@@:UM shopinfo][@@:QU find:first]',$query);
                
	            $row = $shop_info['shopinfo'];			
	            if($row['count_imgsize']>Param::read('upfiles.usersizelimit')*1024*1024){
		            echo("上传图片已超过会员限定数，请清理后上传");
		            exit;
	            }
                if($this->data['staff']['id']) {		
				    $address_id = $this->data['staff']['id'];
			    }				
			    unset($this->data['staff']['id']);
			    $this->data['staff']['shop_id'] = $this->my->id;			
			    $safeData = $this->data['staff'];

		        $query = array();			        			    
                if($address_id) {		
					$query['keyid'] = $address_id;
					$query['set'] = $safeData;
			    }else {
                    $query = $safeData;
				}				    			    				
	            POP::exe('[@@:UM staff][@@:QU save]',$query);

                $this->flash("执行成功！", Router::url('team/staff/'));	
                }else {
                    $this->flash("该员工所属部门和密码不能为空！", Router::url('team/staff/'));
                }				
            }else {
                $this->flash("该员工编号已经存在！", Router::url('team/staff/'));
            }					
        }else {		

			$query = array(
	            'conditions' => array('staff.shop_id' => $this->my->id),
				'fields' => array('staff.*'), 
			);
			$address_rs = POP::exe('[@@:UM staff][@@:QU find:all]',$query);
		
            $user_info=array(
	        'staff_num'=>'',
	        'staff_pwd'=>'',			
	        'name'=>'',
	        'sex'=>'',
	        'jointime'=>'',
	        'birthtime'=>'',
	        'telphone'=>'',
	        'logo'=>'',
	        'jobstr'=>'',
	        'department_id'=>'0',			
            );	

            if($address_id && $address_rs){
	        foreach($address_rs as $value) {
		        if($address_id == $value['staff']['id']) {
			        $user_info = $value['staff'];
		        }
	        }
            }	
        $this->set('departmentcat',$departmentcat);			
        $this->set('user_info',$user_info);		
        $this->set('address_rs',$address_rs);	
        $this->set('address_id',$address_id);
		}
	}	
	
	public function delrem() {	
        $this->_teamRights();	
        $address_id=intval(Request::get_args('address_id'));	

		$query = array(
            'conditions' => array('remuneration.shop_id' => $this->my->id,'remuneration.id' => $address_id),
			'fields' => array('remuneration.*'), 
		);
		$address_rs = POP::exe('[@@:UM remuneration][@@:QU find:first]',$query);

		if(empty($address_rs)) {
            $this->flash("该项不存在!", Router::url('team/remuneration/'));		
		}else {
		    $this->remuneration->delete($address_id);
            $this->flash("执行成功！", Router::url('team/remuneration/'));		   
		}        		
	}	
	
	public function remuneration() {
        $this->_teamRights();	
        $address_id=intval(Request::get_args('address_id'));					
        if(!empty($this->data)) {
            if($this->data['remuneration']['id']) {		
				$address_id = $this->data['remuneration']['id'];
			}

			$query = array(
	            'conditions' => array('remuneration.payofftime' => $this->data['remuneration']['payofftime'],'remuneration.staff_num' => $this->data['remuneration']['staff_num'],'remuneration.shop_id' => $this->my->id),
				'fields' => array('remuneration.*'), 
			);
		    $isarray = POP::exe('[@@:UM remuneration][@@:QU find:first]',$query);
			
			if(empty($isarray) || $address_id) {					
			    unset($this->data['remuneration']['id']);
			    $this->data['remuneration']['shop_id'] = $this->my->id;			
			    $safeData = $this->data['remuneration'];
		        $query = array();			        			    
                if($address_id) {		
					$query['keyid'] = $address_id;
					$query['set'] = $safeData;
			    }else {
                    $query = $safeData;
				}				    			    				
	            POP::exe('[@@:UM remuneration][@@:QU save]',$query);		        
                $this->flash("执行成功！", Router::url('team/remuneration/'));						
            }else {
                $this->flash("该员工薪酬已经存在！", Router::url('team/remuneration/'));
            }					
        }else {	

			$query = array(
	            'conditions' => array('remuneration.shop_id' => $this->my->id),
				'fields' => array('remuneration.*'), 
			);
			$address_rs = POP::exe('[@@:UM remuneration][@@:QU find:all]',$query);
	
	        $user_info=array(
		        'staff_num'=>'',
		        'payofftime'=>'',
		        'jiben'=>'0',
		        'jiangjing'=>'0',
		        'jintie'=>'0',
		        'laowufei'=>'0',
		        'ticheng'=>'0',
		        'fangtie'=>'0',
		        'baoxian'=>'0',
		        'qita'=>'0',
		        'beizhu'=>'',		
	        );	
		
	        if($address_id && $address_rs){
		        foreach($address_rs as $value) {
			        if($address_id == $value['remuneration']['id']) {
				        $user_info = $value['remuneration'];
			        }
		        }
	        }			
	        $this->set('user_info',$user_info);		
	        $this->set('address_rs',$address_rs);	
	        $this->set('address_id',$address_id);
		}
	}	

	public function login() {		
		if (!empty($this->data)) {

			$query = array(
	            'conditions' => array('staff.staff_num' => $this->data['staff']['num'],'staff.staff_pwd' => $this->data['staff']['pwd'],'staff.shop_id' => $this->my->id),
				'fields' => array('staff.*'), 
			);
		    $staffmsg = POP::exe('[@@:UM staff][@@:QU find:first]',$query);
		    		          
		    if ( $staffmsg && $staffmsg['staff']['staff_num'] ==$this->data['staff']['num'] && $staffmsg['staff']['staff_pwd'] ==$this->data['staff']['pwd'] ) {
			    if(!empty($staffmsg['staff']['department_id'])) {
					$query = array(
			            'conditions' => array('department.id' => $staffmsg['staff']['department_id'],'department.shop_id' => $this->my->id),
						'fields' => array('department.*'), 
					);
			        $isarray = POP::exe('[@@:UM staff][@@:QU find:first]',$query);				    
                    if($isarray) {
					    $rightsarray = explode(",",$isarray['department']['rights']);
                        if(empty($rightsarray)){ $rightsarray = array(); }						
						$this->C_session->write('_user_temp.team.rights', $rightsarray);
					    $this->C_session->write('_user_temp.team.login', 'yes');
						$this->flash("提交成功",Router::url('team/login/'));
					}else {
					    $this->flash("该部门不存在或者已经取消，不能登入!", Router::url('team/login/'));
					}
					
				}else {
				    $this->flash("你的员工账号还没有分配部门，请先分配部门再进行此操作！",Router::url('team/login/'));
				}
		    }else {
			    $this->flash("员工账号,或密码错误！", Router::url('team/login/'));
			}
		}
        if($this->C_session->read('_user_temp.team.login') =='yes') {
            $loginstr = '员工切换账号<font color=red>[您已经登录成功]</font>';
        }else {
            $loginstr = '员工登入';
        }
        $this->set('loginstr',$loginstr);	
	}
	
	public function logout() {
        $this->_teamRights();	
		$this->C_session->write('_user_temp.team.login', 'no');	
		$this->C_session->write('_user_temp.team.team_pwd', '');			
		$this->flash("退出成功!", Router::url('team/login/'));
	}
	
	public function teampwdlogin() {	
		if (!empty($this->data)) {
            $curPassWD = $this->user->read('team_pwd');
		    if ( $this->data['user']['pwd'] != $curPassWD['user']['team_pwd'] ) {				
				$this->C_session->write('_user_temp.team.team_pwd', 'yes');
				$this->C_session->write('_user_temp.team.login', 'yes');
			    $this->flash("提交成功",Router::url('team/teampwdlogin/'));
		    }else {
			    $this->flash("密码错误！", Router::url('team/teampwdlogin/'));
			}
		}
        if($this->C_session->read('_user_temp.team.login') =='yes') {
            $loginstr = '最高管理员登入<font color=red>[您已经登录成功]</font>';
			$islogin =1;
			
        }else {
            $loginstr = '最高管理员登入';
			$islogin=0;
        }
        $this->set('loginstr',$loginstr);	
        $this->set('islogin',$islogin);		
	}
	
	public function teampwd() {	
		if (!empty($this->data)) {
			if($this->data['user']['teampwd'] == $this->data['user']['checkpwd']) {		
			    $this->user->id = $this->my->id;
			    $this->user->set( $this->data );
			    if ($this->user->save(array('team_pwd' => $this->data['user']['teampwd']))) {
				    $this->flash("提交成功!", Router::url('team/teampwd/'));
			    } else {
			        $this->flash("提交失败!", Router::url('team/teampwd/'));
			    }
			}else {
			    $this->flash("两次输入的新密码不同!", Router::url('team/teampwd/'));
			}			
		}
	}	

	public function teampwdedit() {	
		if (!empty($this->data)) {
			$this->user->id = $this->my->id;
			if($this->data['user']['teampwd'] == $this->data['user']['checkpwd']) {
			    $curPassWD = $this->user->read('team_pwd');
			    if ($this->data['user']['oldpwd'] != $curPassWD['user']['team_pwd']) {
                    $this->flash("旧密码错误!", Router::url('team/teampwdedit/'));
			    } else {
				    $this->user->set( $this->data );
				    if ($this->user->save(array('team_pwd' => $this->data['user']['teampwd']))) {
                        $this->flash("提交成功!", Router::url('team/teampwdedit/'));
				    } else {
                        $this->flash("提交失败!", Router::url('team/teampwdedit/'));
				    }
			    }
			}else {
			    $this->flash("两次输入的新密码不同!", Router::url('team/teampwdedit/'));
			}
		}	
	}	
	
}

?>